package com.snowfield.lb47.lbconfig.service.impl;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.snowfield.lb47.lbconfig.domain.BackserverCrt;
import com.snowfield.lb47.lbconfig.mapper.BackserverCrtMapper;
import com.snowfield.lb47.lbconfig.service.IBackserverCrtService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * 服务器证书Service业务层处理
 *
 * @author snowfield
 * @date 2024-03-25
 */
@Service
public class BackserverCrtServiceImpl implements IBackserverCrtService {
    @Autowired
    private BackserverCrtMapper backserverCrtMapper;

    /**
     * 查询服务器证书
     *
     * @param id 服务器证书主键
     * @return 服务器证书
     */
    @Override
    public BackserverCrt selectBackserverCrtById(Long id) {
        return backserverCrtMapper.selectBackserverCrtById(id);
    }

    /**
     * 查询服务器证书列表
     *
     * @param backserverCrt 服务器证书
     * @return 服务器证书
     */
    @Override
    public List<BackserverCrt> selectBackserverCrtList(BackserverCrt backserverCrt) {
        return backserverCrtMapper.selectBackserverCrtList(backserverCrt);
    }

    /**
     * 新增服务器证书
     *
     * @param backserverCrt 服务器证书
     * @return 结果
     */
    @Override
    public int insertBackserverCrt(BackserverCrt backserverCrt) {
        backserverCrt.setCreateTime(DateUtils.getNowDate());
        return backserverCrtMapper.insertBackserverCrt(backserverCrt);
    }

    /**
     * 修改服务器证书
     *
     * @param backserverCrt 服务器证书
     * @return 结果
     */
    @Override
    public int updateBackserverCrt(BackserverCrt backserverCrt) {
        backserverCrt.setUpdateTime(DateUtils.getNowDate());
        return backserverCrtMapper.updateBackserverCrt(backserverCrt);
    }

    /**
     * 批量删除服务器证书
     *
     * @param ids 需要删除的服务器证书主键
     * @return 结果
     */
    @Override
    public int deleteBackserverCrtByIds(Long[] ids) {
        //记录待删除的证书文件路径
        List<String> delFilePath = new ArrayList<>();
        for (Long id : ids) {
            BackserverCrt backserverCrt = backserverCrtMapper.selectBackserverCrtById(id);

            if (backserverCrt == null) {
                continue;
            }
            //判断服务器证书是否已经分配，若已经分配不可以删除
            int count = backserverCrtMapper.usedServerCrtCount(backserverCrt.getServerId(), backserverCrt.getCrtPort());
            if (count > 0) {
                throw new ServiceException(String.format("%1$s已分配,不能删除", backserverCrt.getCrtPort()));
            }

            String crtPath = backserverCrt.getCrtPath();
			delFilePath.add(crtPath);
        }
        //删除证书记录
        Integer result = backserverCrtMapper.deleteBackserverCrtByIds(ids);
        if (result > 0) {
            for (String crtPath : delFilePath) {
                File crtFile = new File(crtPath);
                if (crtFile.exists()) {
                    FileUtils.deleteQuietly(crtFile);
                }
            }
        }
        return result;
    }

    /**
     * 删除服务器证书信息
     *
     * @param id 服务器证书主键
     * @return 结果
     */
    @Override
    public int deleteBackserverCrtById(Long id) {
        String crtPath = backserverCrtMapper.selectBackserverCrtById(id).getCrtPath();
        Integer result = backserverCrtMapper.deleteBackserverCrtById(id);
        if (result > 0) {
            File crtFile = new File(crtPath);
            if (crtFile.exists()) {
                FileUtils.deleteQuietly(crtFile);
            }
        }
        return result;
    }

    /**
     * 校验服务器证书是否唯一
     *
     * @param backserverCrt 服务器证书
     * @return 结果
     */
    public Boolean checkPortUnique(BackserverCrt backserverCrt) {
        Long crtId = backserverCrt.getId();
        Long serverId = StringUtils.isNull(backserverCrt.getServerId()) ? -1L : backserverCrt.getServerId();
        Long crtPort = backserverCrt.getCrtPort();
        BackserverCrt crt = backserverCrtMapper.selectBackserverCrt(serverId, crtPort);
        if (StringUtils.isNotNull(crt) && !crt.getId().equals(crtId)) {
            return Boolean.FALSE;
        }
        return Boolean.TRUE;
    }
}
